<?php

/**
 * If you are not authorized to modify this file, do NOT touch it.
 * @file Exception.class.php
 * @author cble
 * @date 2010-12-2
 * @description This file contains the definition for exception handling.
 */

if (!defined('IN_IMBA')) exit('Access Denied.');

/**
 * The following definitions are for the custom exception class.
 */
class ImbaException extends Exception
{
	//Enable the user to store extra information of exception.
	private $abbr;
	private $extraInfo;
	
	/**
     * @name __construct
     * @author cble
     * @date 2010-12-3
     * @description
     *   The function is the rewrited version of the function in the parent class.
     * @parameters
     *   string $message: The exception message when exception is thrown.
     *   integer $code: The exception code when exception is thrown.
     *   mixed $extraInfo: The extra information when exception is thrown.
     * @return void
     */
	public function __construct($code = E_UNKNOWN, $extraInfo = '')
    {
    	global $e_detail;
    	//The following judgement prevents undefined exception code.
        /*
		if (!($code>=0 && $code<=E_TYPE_UNKNOWN))
        {
        	$code = E_TYPE_UNKNOWN;
        }*/
        if (!isset($e_detail[$code])) $code = E_UNKNOWN;
        $this->abbr = $e_detail[$code]['abbr'];
		$this->extraInfo = $extraInfo;
        parent::__construct($e_detail[$code]['msg'], $code);
    }
    
    /**
     * @name __toString
     * @author cble
     * @date 2010-12-3
     * @description
     *   The function is the rewrited version of the function in the parent class.
     *   The function prints the detail of exception.
     * @parameters void
     * @return string $exceptionInfo: The string of detailed exception information.
     */
    public function __toString()
    {
    	global $e_detail;
  		$exceptionInfo = 'Caught Exception: ';
        if (IMBA_DEBUG)
        {
  		    $exceptionInfo .= $this->getCode() . ' : ' . $e_detail[$this->getCode()]['abbr'] . ' at "' . $this->getFile() . '" on Line ' . $this->getLine() . '<br />';
  		    $exceptionInfo .= $e_detail[$this->getCode()]['msg'];
        }
        else
        {
            $exceptionInfo .= $this->getCode() . ' : ' . $e_detail[$this->getCode()]['abbr'] . '<br />';
  		    $exceptionInfo .= 'Turn on debug mode to view detailed information.';
        }   
  		return $exceptionInfo;
    }
    
    /**
     * @name GetExtraInfo
     * @author cble
     * @date 2010-12-3
     * @description
     *   The function enables user to get the extra information back so that he/she can deal with it.
     * @parameters void
     * @return mixed $extraInfo: The extra information given by the user when constructing.
     */
    public function GetExtraInfo()
    {
        return $this->extraInfo;
    }
    
    /**
     * @name IncludeExceptionHandler
     * @author cble
     * @date 2010-12-4
     * @description
     *   The function enables user to include different exception-handling page due to different exception.
     *   Once calling this function, the application will be stopped.
     * @parameters void
     * @return void
     */
    public function IncludeExceptionHandlerStop()
    {
  		include ('Exception.php');
  		exit;
    }
    
    /**
     * @name IncludeExceptionHandler
     * @author cble
     * @date 2010-12-6
     * @description
     *   The function enables user to include different exception-handling page due to different exception.
     *   Once calling this function, the application will NOT be stopped.
     * @parameters void
     * @return void
     */
    public function IncludeExceptionHandlerContinue()
    {
  		include ('Exception.php');
    }
}

?>